package com.edu.course.service.impl;


import com.api.sso.SSOClient;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.edu.course.mapper.HfdClassInfoMapper;
import com.edu.course.mapper.HfdClassSkuInfoMapper;
import com.edu.course.service.IHfdClassSkuInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.edu.entity.HfdClassInfo;
import com.edu.entity.HfdClassSkuInfo;
import com.edu.entity.HfdUserInfo;
import com.edu.interceptor.UserInfoInterceptor;
import com.edu.utils.JsonUtils;
import com.edu.vo.Result;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author jr
 * @since 2021-04-14
 */
@Service
public class HfdClassSkuInfoServiceImpl extends ServiceImpl<HfdClassSkuInfoMapper, HfdClassSkuInfo> implements IHfdClassSkuInfoService {

    @Autowired(required = false)
    private HfdClassSkuInfoMapper hfdClassSkuInfoMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public List<HfdClassSkuInfo> findClassSkuInfoByCid(Integer cid) {

        BoundHashOperations<String, Object, Object> ops = stringRedisTemplate.boundHashOps("hot:sku" + cid);

        List<Object> values = ops.values();


        List<HfdClassSkuInfo> skuInfo = new ArrayList<>();

        if (values == null || values.size() == 0) {

            List<HfdClassSkuInfo> skus = new ArrayList<>();

            synchronized (this) {
                //再次从redis中获取
                List<Object> valuesAgain = ops.values();

                if (valuesAgain == null || valuesAgain.size() == 0) {
                    //如果redis中没有，从数据库中获取，
                    System.out.println("从数据库中获取");

                    //从数据库获取
                    skus = hfdClassSkuInfoMapper.findClassSkuInfoByCid(cid);

                    //缓存一份到数据库
                    skus.forEach(sku -> {
                        ops.put(sku.getSkuId() + "", JsonUtils.toString(sku));
                    });

                } else {
                    //redis中有
                    System.out.println("从redis中获取");
                    for (Object value : valuesAgain) {
                        HfdClassSkuInfo hfdClassSkuInfo = JsonUtils.toBean((String) value, HfdClassSkuInfo.class);
                        skus.add(hfdClassSkuInfo);
                    }
                }

            }
            return skus;

        } else {

            //如果redis中有，直接返回
            System.out.println("从redis中获取");
            for (Object value : values) {
                HfdClassSkuInfo hfdClassSkuInfo = JsonUtils.toBean((String) value, HfdClassSkuInfo.class);
                skuInfo.add(hfdClassSkuInfo);
            }

            return skuInfo;

        }


    }

    @Override
    public HfdClassSkuInfo findClassSkuInfoBySkuId(Integer skuId) {

        BoundHashOperations<String, Object, Object> ops = stringRedisTemplate.boundHashOps("hot:sku");


        Object o = ops.get(skuId + "");

        if (o == null) {
            //redis中没有,直接差数据库中class_sku表到所有信息
            HfdClassSkuInfo skuInfo = hfdClassSkuInfoMapper.findClassSkuInfoBySkuId(skuId);

            //缓存到redis
            ops.put(skuInfo.getSkuId() + "", JsonUtils.toString(skuInfo));


            Object o1 = ops.get(skuId + "");

            return JsonUtils.toBean((String) o1, HfdClassSkuInfo.class);

        } else {
            //redis中有，直接返回
            Object o2 = ops.get(skuId + "");
            return JsonUtils.toBean((String) o2, HfdClassSkuInfo.class);
        }

    }

    /**
     * 获取当前登录用户已购买的课程信息
     *
     * @return
     */
    @Override
    public Result getMyClass() {
        HfdUserInfo userInfo = UserInfoInterceptor.getUserInfo();
        List<HfdClassSkuInfo> classSkuInfoList = hfdClassSkuInfoMapper.findClassInfoByUserId(userInfo.getUserId());
        if (classSkuInfoList == null) {
            return new Result(false, 0, "查询失败");
        }
        return new Result(true, 1, "查询成功", classSkuInfoList);
    }
}
